#include<stdio.h>
#include<string.h>

void clearStr(char s[])
{
    int i=0;
    while(s[i]!='\0')
    {
        s[i]='\0';
        i++;
    }
}
int ReadNum()
{
    FILE *fp;
    if((fp = fopen("numb.txt", "r")) == NULL)
    {
        printf("\nCan not open file numb.txt");
        exit(1);
    }
    int num;
    fscanf(fp, "%d", &num);
    fclose(fp);
    return num;
}

int WriteFile(char headName[], int num,char matrixMap[30][30], int m, int n)
{
    int i, j;
    char name[30], strNum[20];
    clearStr(name);
    clearStr(strNum);
    sprintf(strNum, "%d", num);
    strcat(name, headName);
    strcat(name, strNum);
    printf("%s", name);
    FILE *fp;
    if((fp = fopen(name, "w")) == NULL)
    {
        return 0;
    }
    fprintf(fp, "%d %d\n", m, n);
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            fprintf(fp, "%c", matrixMap[i][j]);
        }
        fprintf(fp, "\n");
    }
    fclose(fp);
    return 1;
}

void ChangeNumber(int number)
{
    FILE *fp;
    if((fp = fopen("numb.txt", "w")) == NULL)
    {
        printf(" Can not write file numb.txt");
    }
    fprintf(fp, "%d", number+1);
    fclose(fp);
}

int SaveMap(char headName[], char matrixMap[30][30], int m, int n)
{
    int num;
    num = ReadNum();
    //printf("%d", num);
    if(!WriteFile(headName, num+1, matrixMap, m, n))
    {
        printf("\nCan not write file %s%d", headName, num+1);
    }

    ChangeNumber(num);
}

int main()
{
    char matrixMap[30][30];
    char headName[15];
    clearStr(headName);
    headName[0] = 'n';
    headName[1] = 'o';
    //printf("%s", headName);
    matrixMap[0][0]='1';
    matrixMap[0][1]='3';
    matrixMap[1][0]='2';
    matrixMap[1][1]='4';
    matrixMap[2][0]='2';
    matrixMap[2][1]='1';
    SaveMap(headName, matrixMap, 3, 2);
}
